/**
 * SSTableModelListener.java
 */
package com.pluginsacceptor.plugin.spreadsheet.table;

import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;

import com.pluginsacceptor.plugin.spreadsheet.SSHandler;
import com.pluginsacceptor.plugin.spreadsheet.model.Cell;
import com.pluginsacceptor.utils.CommunicationUtils;

/**
 * The spread sheet table model listener.
 * Listen data changes and cell focus
 */
public class SSTableModelListener implements TableModelListener {


    /** */
    private final SSHandler handler;

    /**
     * Constructor
     * @param handler
     */
    public SSTableModelListener(final SSHandler handler) {
        super();
        this.handler = handler;
    }


    @Override
    public void tableChanged(final TableModelEvent event) {
        final int row = event.getFirstRow();
        final int column = event.getColumn();

        if (row != 0 && column != 0) {
            final TableModel model = (TableModel)event.getSource();
            final String data = ((Cell) model.getValueAt(row, column)).getText();
            this.handler.notifyPluginObservers(
                CommunicationUtils.cellDataToString(row, column, data)
            );
//           System.out.println("row " + row + " col " + column);
        }

    }

}
